Demographics

Row

Community population by ethnicity

Community population by race

Row

Ethnicity distribution within community

Race distribution within community

Income

Percentage of HH with income above the median

Percentage of HH by income bracket

Percentage of HH by income bracket

---
title: "Racial Segregation and Income Disparities"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    theme: simplex
    source_code: embed
---


```{r setup, include=FALSE}
library(ggplot2)
library(tidyverse)
library(dplyr)
library(gridExtra)
library(plotly)
library(plyr)
library(flexdashboard)

#working directory
setwd("~/Dropbox/HARRIS/Spring 2018/DATA VIZ/Final paper/Flex")

#Read csv
ethnicity_tidy <- read.csv("ethnicity_tidy.csv")

new_data_ethnicity_percent_tidy <- read.csv("new_data_ethnicity_percent_tidy.csv")

race_tidy <- read.csv("race_tidy.csv")

new_data_race_percent_tidy <- read.csv("new_data_race_percent_tidy.csv")
```



Demographics
=======================================================================

Row
-----------------------------------------------------------------------

### Community population by ethnicity

```{r}
dem_1 <- ethnicity_tidy %>%
  plot_ly(x = ~place, y = ~ ethnicity_tidy$Population, color = ~Ethnicity) %>%
   layout(xaxis = list(
           title = ""), margin = list(b = 70),
         yaxis = list(title = "Population"))


dem_1
```


### Community population by race

```{r}
dem_3 <- race_tidy %>%
  plot_ly(x = ~place, y = ~ race_tidy$Population, color = ~Race) %>%
   layout(xaxis = list(
           title = ""), margin = list(b = 70),
         yaxis = list(title = "Population"))


dem_3
```


Row
-----------------------------------------------------------------------

### Ethnicity distribution within community

```{r}
dem_2 <- new_data_ethnicity_percent_tidy %>%
plot_ly(x = ~Ethnicity, y = ~ new_data_ethnicity_percent_tidy$Percentage, color = ~place )%>%
   layout(
         yaxis = list(title = "Percentage of people"))

dem_2
```


### Race distribution within community
```{r}
dem_4 <- new_data_race_percent_tidy %>%
  plot_ly(x = ~Race, y = ~ new_data_race_percent_tidy$Percent, color = ~place )%>%
   layout(
         yaxis = list(title = "Percentage of people"))



dem_4
```


Income
=======================================================================

### Percentage of HH with income above the median

```{r}
above_median_HH <- read.csv("above_median_per.csv")


above_median_HH$Race <- factor(above_median_HH$Race,
                   levels = c('Asian', 'White', 'All', 'Hispanic', 'Other', 'Black'))

inc_1 <- plot_ly(above_median_HH, x = ~Race, y = ~Percentage, type = 'bar',
        marker = list(color = 'rgba(222,45,38,0.8)',
                      line = list(color = I("black"),
                                  width = 1.5))) %>%
  layout(
       xaxis = list(title = "Race"),
        yaxis = list(title = "Percentage of HH"))


 inc_1
```

### Percentage of HH by income bracket

```{r}
tidy_income_race_pop <- read.csv("tidy_percent_pop_tot2.csv")

tidy_income_race_pop$Income <- factor(tidy_income_race_pop$Income,
                   levels = c('$50,000 to $59,999', '$60,000 to $74,999', '$75,000 to $99,999', '$100,000 to $124,999', '$125,000 to $149,999', '$150,000 to $199,999', '$200,000 or more' ))

inc_2 <- plot_ly(tidy_income_race_pop, x = ~Percentage, y = ~Income, color = ~Race) %>%
  layout(xaxis = list(title = ''), yaxis = list(title = ''), barmode = 'stack', margin = list(l = 150, r = 0, t = 40, b = 5), legend = list(orientation = "h",   
                     xanchor = "center",
                     x = 0.5))
inc_2
```   
    
### Percentage of HH by income bracket

```{r}

tidy_income_hisp <- read.csv("tidy_percent_pop_tot2_Hisp.csv")

tidy_income_hisp$Income <- factor(tidy_income_hisp$Income,
                   levels = c('$50,000 to $59,999', '$60,000 to $74,999', '$75,000 to $99,999', '$100,000 to $124,999', '$125,000 to $149,999', '$150,000 to $199,999', '$200,000 or more' ))

inc_3 <- plot_ly(tidy_income_hisp, x = ~Percentage, y = ~Income, color = ~Race) %>%
  layout(xaxis = list(title = ''), yaxis = list(title = ''), barmode = 'stack', margin = list(l = 150, r = 0, t = 40, b = 5), legend = list(orientation = "h",   
                     xanchor = "center",
                     x = 0.5))
inc_3
```